package com.ruoyi.quotation.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quotation.domain.QuoEngineering;
import com.ruoyi.quotation.domain.QuoQuote;
import com.ruoyi.quotation.mapper.QuoEngineeringMapper;
import com.ruoyi.quotation.mapper.QuoQuoteMapper;
import com.ruoyi.quotation.service.IQuoEngineeringService;
import com.ruoyi.quotation.vo.QuoteEngineeringVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 工程审核服务实现
 */
@Service
public class QuoEngineeringServiceImpl implements IQuoEngineeringService {

    @Autowired
    private QuoEngineeringMapper quoEngineeringMapper;

    @Autowired
    private QuoQuoteMapper quoQuoteMapper;

    /**
     * 查询工程审核列表
     *
     * @param quoEngineering 工程审核信息
     * @return 工程审核列表
     */
    @Override
    public List<QuoteEngineeringVO> selectQuoEngineeringList(QuoEngineering quoEngineering) {
        return quoEngineeringMapper.selectQuoEngineeringList(quoEngineering);
    }

    /**
     * 根据ID查询工程审核信息
     *
     * @param id 工程审核ID
     * @return 工程审核信息
     */
    @Override
    public QuoEngineering selectQuoEngineeringById(Integer id) {
        return quoEngineeringMapper.selectQuoEngineeringById(id);
    }

    /**
     * 新增工程审核
     *
     * @param quoEngineering 工程审核信息
     * @return 结果
     */
    @Override
    public int insertQuoEngineering(QuoEngineering quoEngineering) {
        quoEngineering.setCreateBy(SecurityUtils.getUsername());
        quoEngineering.setCreateTime(new Date());
        return quoEngineeringMapper.insertQuoEngineering(quoEngineering);
    }

    /**
     * 修改工程审核
     *
     * @param quoEngineering 工程审核信息
     * @return 结果
     */
    @Override
    public int updateQuoEngineering(QuoEngineering quoEngineering) {
        quoEngineering.setUpdateBy(SecurityUtils.getUsername());
        quoEngineering.setUpdateTime(new Date());
        return quoEngineeringMapper.updateQuoEngineering(quoEngineering);
    }

    /**
     * 批量删除工程审核
     *
     * @param ids 需要删除的工程审核ID
     * @return 结果
     */
    @Override
    public int deleteQuoEngineeringByIds(Integer[] ids) {
        return quoEngineeringMapper.deleteQuoEngineeringByIds(ids);
    }

    /**
     * 执行工程审核
     *
     * @param quoEngineering 工程审核信息
     * @return 结果
     */
    @Override
    @Transactional
    public int auditEngineering(QuoEngineering quoEngineering) {
        // 1. 新增或更新工程审核信息
        QuoEngineering exist = quoEngineeringMapper.selectQuoEngineeringByOrderNo(quoEngineering.getOrderNo());
        int rows = 0;
        
        if (exist != null) {
            quoEngineering.setId(exist.getId());
            quoEngineering.setUpdateBy(SecurityUtils.getUsername());
            quoEngineering.setUpdateTime(new Date());
            rows = quoEngineeringMapper.updateQuoEngineering(quoEngineering);
        } else {
            quoEngineering.setCreateBy(SecurityUtils.getUsername());
            quoEngineering.setCreateTime(new Date());
            rows = quoEngineeringMapper.insertQuoEngineering(quoEngineering);
        }
        
        // 2. 更新报价单状态为"财务审核中"
        if (rows > 0) {
            QuoQuote quoQuote = new QuoQuote();
            quoQuote.setOrderNo(quoEngineering.getOrderNo());
            quoQuote.setStatus("财务审核中");
            quoQuote.setUpdateBy(SecurityUtils.getUsername());
            quoQuoteMapper.updateQuoteStatusByOrderNo(quoQuote);
        }
        
        return rows;
    }
} 